package com.thiswhyme.common.utils.codec;

import com.thiswhyme.common.expection.BusinessException;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @description: Base64字符串等互转工具
 * @date: 2021/9/13
 */
@Slf4j
public class CodecUtils {

    /**
     * 字符串转Base64字节数组
     * @param src
     * @return
     */
    public static byte[] string2Base64Bytes(String src) {
        return bytes2Base64Bytes(src.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 字符串转Base64字符串
     * @param src
     * @return
     */
    public static String string2Base64String(String src) {
        String result = null;
        if (!(src == null || src.length() == 0)) {
            result = new String(string2Base64Bytes(src), StandardCharsets.UTF_8);
        }
        return result;
    }

    /**
     * 字节数组转Base64字节数组
     * @param src
     * @return
     */
    public static byte[] bytes2Base64Bytes(byte[] src) {
        byte[] result = null;
        if (src != null && src.length > 0) {
            result = Base64.getEncoder().encode(src);
        }
        return result;
    }

    /**
     * 字节数组转Base64字符串
     * @param src
     * @return
     */
    public static String bytes2Base64String(byte[] src) {
        return new String(bytes2Base64Bytes(src), StandardCharsets.UTF_8);
    }

    /**
     * Base64字符串转字节数组
     * @param src
     * @return
     */
    public static byte[] base64String2Bytes(String src) {
        byte[] result = null;
        if (!(src == null || src.length() == 0)) {
            result = base64Bytes2Bytes(src.getBytes(StandardCharsets.UTF_8));
        }
        return result;
    }

    /**
     * Base64字符串转字符串
     * @param src
     * @return
     */
    public static String base64String2String(String src) {
        String result;
        if (!(src == null || src.length() == 0)) {
            result = "";
        } else {
            result = new String(base64String2Bytes(src), StandardCharsets.UTF_8);
        }
        return result;
    }

    /**
     * Base64字节数组转字节数组
     * @param src
     * @return
     */
    public static byte[] base64Bytes2Bytes(byte[] src) {
        return Base64.getDecoder().decode(src);
    }

    /**
     * Base64字节数组转字符串
     * @param src
     * @return
     */
    public static String base64Bytes2String(byte[] src) {
        return new String(base64Bytes2Bytes(src), StandardCharsets.UTF_8);
    }

    /**
     * 字节数组转16进制字符串（大写）
     * @param src
     * @return
     */
    public static String byte2UppercaseHexString(byte[] src) {
        return CodecUtils.byte2HexString(src).toUpperCase();
    }

    /**
     * 字节数组转16进制字符串
     * @param src
     * @return
     */
    public static String byte2HexString(byte[] src) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : src) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

    /**
     * 16进制字符串转字节数组
     * @param s
     * @return
     */
    public static byte[] hexStringToByte(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                log.error("AES解密异常", e);
                throw new BusinessException("AES解密异常");
            }
        }
        return baKeyword;
    }
}
